package com.hi.hiparty.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.hi.hiparty.R;
import com.hi.hiparty.ui.session.UserSession;
import com.hi.hiparty.ui.session.UserSessionManager;
import com.hi.hiparty.util.ApiHelper;
import com.hi.hiparty.util.HttpUtil;
import com.hi.hiparty.util.StringUtils;

public class RegisterFillNicknameActivity extends Activity {

	private int gender = -1;

	private String userid;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_fillnick);

		Bundle bundle = new Bundle();
		bundle = this.getIntent().getExtras();
		userid = bundle.getString("userid");
	}

	public void onRadioButtonClicked(View view) {
		// Is the button now checked?
		boolean checked = ((RadioButton) view).isChecked();

		// Check which radio button was clicked
		switch (view.getId()) {
		case R.id.radio_gender_male:
			if (checked)
				gender = 1;
			break;
		case R.id.radio_gender_female:
			if (checked)
				gender = 0;
			break;
		}
	}

	public void onNextButtonClick(View view) {
		EditText nickTextInput = (EditText) findViewById(R.id.edit_text_nickname);
		String nick = nickTextInput.getText().toString();

		if (StringUtils.isBlank(nick)) {
			showErrorMessageToast("昵称不能为空");
			return;
		}

		if (nick.length() < 4) {
			showErrorMessageToast("昵称长度必须大于4个字");
			return;
		}

		if (gender == -1) {
			showErrorMessageToast("请选择性别");
			return;
		}

		FillNickAsyncTask fillNickAsyncTask = new FillNickAsyncTask();
		fillNickAsyncTask.execute(nick, "" + gender);
	}

	private void showErrorMessageToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	public class FillNickAsyncTask extends AsyncTask<String, Void, Boolean> {

		private ProgressDialog dialog;

		private String errorMessage;

		protected void onPreExecute() {
			dialog = ProgressDialog.show(RegisterFillNicknameActivity.this, "", "正在发送服务器验证信息...", true);
		}

		protected void onPostExecute(final Boolean success) {
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			if (success) {
				// 进入登陆后主页
				Intent intent = new Intent(RegisterFillNicknameActivity.this, MainActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("userid", userid);
				intent.putExtras(bundle);
				startActivity(intent);
			} else {
				Toast.makeText(RegisterFillNicknameActivity.this, errorMessage, Toast.LENGTH_LONG).show();
			}
		}

		protected Boolean doInBackground(final String... args) {
			if (null == args || args.length < 2) {
				return false;
			}
			String nick = args[0];
			String gender = args[1];
			String url = ApiHelper.API_FILL_NICKNAME;
			Map<String, String> params = new HashMap<String, String>();
			params.put("userid", userid);
			params.put("nickname", nick);
			params.put("gender", gender);
			String jsonString = HttpUtil.POST(url, params);
			if (StringUtils.isBlank(jsonString)) {
				errorMessage = "您的网络信号不太好";
				return false;
			}
			try {
				JSONObject json = new JSONObject(jsonString);
				if (json.getInt("success") == 1) {
					UserSession session = UserSessionManager.createNewSession();
					session.setUserid(userid);
					UserSessionManager.saveUserSession(RegisterFillNicknameActivity.this);
					return true;
				} else {
					errorMessage = json.getString("error");
					return false;
				}
			} catch (JSONException e) {
				errorMessage = "服务请求格式异常，请稍后再试";
				return false;
			}
		}
	}
}
